package com.example.weatherpromax.ui.weather

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import com.example.weatherpromax.logic.Repository
import com.example.weatherpromax.logic.model.Location

class WeatherViewModel : ViewModel() {

    //创建locationLivaData对象，类型为Location
    private val locationLiveData = MutableLiveData<Location>()

    //用于缓存的变量
    var locationLng = ""

    var locationLat = ""

    var placeName = ""

    //用于观察locationLiveData对象的情况，每当refreshWeather函数被调用时，会同时执行switchMap方法，将query数值传给repository的三个天气查询方法方法，实现网络请求的发起
    val weatherLiveData = Transformations.switchMap(locationLiveData) { location -> Repository.refreshWeather(location.lng, location.lat)}


    fun refershWeather(lng: String, lat: String) {
        locationLiveData.value = Location(lng, lat)
    }
}